package com.bilin.coin.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.ApiConstant;
import com.bilin.coin.R;
import com.bilin.coin.bean.CoinBean;
import com.bilin.coin.bean.MyCoinParamBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.network.RequestUtils;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.TimerUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


@Route(path = "/order/WithdrawCoinActivity")
public class WithdrawCoinActivity extends BaseAppCompatActivity {

    @BindView(R.id.et_amount)
    EditText et_amount;
    @BindView(R.id.et_address)
    EditText et_address;
    @BindView(R.id.et_pwd)
    EditText et_pwd;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_available)
    TextView tv_available;
    @BindView(R.id.tv_miner_money)
    TextView tv_miner_money;
    @BindView(R.id.bt_send)
    TextView bt_send;
    @BindView(R.id.tv_title_info1)
    TextView tv_title_info1;
    @BindView(R.id.tv_title_info2)
    TextView tv_title_info2;
    @BindView(R.id.tv_hyd)
    TextView tv_hyd;

    @Autowired
    CoinBean coinBean;

    private MyCoinParamBean myCoinParamBean;

    @Override
    protected void initView() {
        setTitle("提"+coinBean.getName());
        setMenu("提"+coinBean.getName()+"纪录");
        tv_title_info1.setText("提"+coinBean.getName()+"数量");
        tv_title_info2.setText("提"+coinBean.getName()+"地址");
        getData();
    }

    private void getData() {
        new Geter(this) {
            @Override
            protected String fillUrl() {
                return "/api/coin/withdrawParams";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap<String, String> stringHashMap = new HashMap<>();
                stringHashMap.put("id",coinBean.getId());
                return stringHashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                myCoinParamBean = RequestUtils.getGson().fromJson(dataJson, MyCoinParamBean.class);
                fillData(myCoinParamBean);
            }


        };
    }

    private void fillData(MyCoinParamBean myCoinParamBean) {
        tv_available.setText(getResources().getString(R.string.usable_amount)
                + myCoinParamBean.getUsable().toPlainString() + coinBean.getName());
        et_amount.setHint(myCoinParamBean.getMin()+coinBean.getName()+"起提"+",最大提币数量"+ myCoinParamBean.getUsable().toPlainString()+coinBean.getName()+"");
        tv_miner_money.setText("矿工费："+myCoinParamBean.getFee());
        if (Double.parseDouble(myCoinParamBean.getActivation()) >0) {
            tv_hyd.setVisibility(View.VISIBLE);
            tv_hyd.setText("消耗活跃度："+myCoinParamBean.getActivation());
        } else {
            tv_hyd.setVisibility(View.GONE);
        }
     /*   try {
            DecimalFormat df = new DecimalFormat("0.00%");
            String[] split = myCoinParamBean.getFee().split("\\+");
            String bai = df.format(Double.parseDouble(split[0]));
            tv_miner_money.setText(getResources().getString(R.string.miner_fee)
                    + bai+"+"+split[1] +coinBean.getName());
        } catch (Exception e) {
            e.printStackTrace();
            tv_miner_money.setText(getResources().getString(R.string.miner_fee)
                    +"0.00%+2" + coinBean.getName());
        }*/

    }

    @Override
    public void setMenuOnClickListener() {
        jumpRecord();

    }

    private void jumpRecord() {
        baseStartActivityWith("/order/RechargeWithdrawCoinRecordActivity")
                .withInt("type", 1)
                .withParcelable("coinBean",coinBean)
                .navigation();
    }


    @Override
    protected void initData() {

    }


    @OnClick({R.id.btn_submit, R.id.tv_all, R.id.bt_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                submit();
                break;
            case R.id.tv_all:
                if (myCoinParamBean != null) {
                    et_amount.setText(myCoinParamBean.getUsable().toPlainString());
                }
                break;
            case R.id.bt_send:
                sendCode();
                break;
        }
    }

    private void submit() {
        String amount = et_amount.getText().toString().trim();
        String address = et_address.getText().toString().trim();
        String safeWord = et_pwd.getText().toString().trim();
        String code = et_code.getText().toString().trim();
        new Poster(this) {
            @Override
            protected String fillUrl() {
                return "/api/coin/withdraw";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("address", address);
                hashMap.put("amount", amount);
                hashMap.put("code", code);
                hashMap.put("id", coinBean.getId()+"");
                //hashMap.put("safeWord", safeWord);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                jumpRecord();
                finish();
            }
        };
    }

    /**
     * 发送短信验证码
     */
    public void sendCode() {
        new Poster(this) {
            @Override
            protected String fillUrl() {
                return ApiConstant.INNER_CODE;
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap params = new HashMap();
                return params;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                TimerUtils.getInitialise().startTimer(bt_send, 120, getString(R.string.login_resend_code));
            }
        };
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdraw_coin;
    }

}
